package com.billlv.codegenerator.controller;

import com.billlv.codegenerator.common.utils.Result;
import com.billlv.codegenerator.domain.entity.TableFieldMetadata;
import com.billlv.codegenerator.service.TableFieldMetaDataService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/columns")
public class ColumnController {

    private final TableFieldMetaDataService tableFieldMetaDataService;

    public ColumnController(TableFieldMetaDataService tableFieldMetaDataService) {
        this.tableFieldMetaDataService = tableFieldMetaDataService;
    }


    /**
     * Retrieve a specific ProjectConfig by ID.
     *
     * @param id the ID of the ProjectConfig to retrieve
     * @return the ProjectConfig details wrapped in a Result
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<TableFieldMetadata>> read(@PathVariable Long id) {
        TableFieldMetadata result = tableFieldMetaDataService.read(id);
        return ResponseEntity.ok(Result.success(result));
    }


    /**
     * Update an existing ProjectConfig.
     *
     * @param id the ID of the ProjectConfig to update
     * @param dto the updated ProjectConfig data transfer object
     * @return the updated ProjectConfig wrapped in a Result
     */
    @PutMapping("/{id}")
    public ResponseEntity<Result<TableFieldMetadata>> update(@PathVariable Long id,  @RequestBody TableFieldMetadata dto) {
        TableFieldMetadata updatedVO = tableFieldMetaDataService.update(id, dto);
        return ResponseEntity.ok(Result.success(updatedVO));
    }

    /**
     * Delete a specific ProjectConfig by ID.
     *
     * @param id the ID of the ProjectConfig to delete
     * @return success response wrapped in a Result
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> delete(@PathVariable Long id) {
        tableFieldMetaDataService.delete(id);
        return ResponseEntity.ok(Result.success(null));
    }


    @PostMapping("/sync/{tableName}")
    public String syncMetadata(@PathVariable String tableName) {
        tableFieldMetaDataService.syncColumnByTableName(tableName);
        return "Metadata synchronization completed successfully!";
    }


}
